package models

import (
	"fmt"
	"strconv"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

type CaseBook struct {
	gorm.Model
	CaseStuID     string `json:"casestuid" gorm:"type:varchar(20) not null; unique"`
	CaseStuName   string `json:"casestuname" gorm:"type:varchar(20) not null;"`
	CaseStuAge    int    `json:"casestuage" gorm:"type:integer not null;"`
	CaseStuSex    string `json:"casestusex" gorm:"type:varchar(20) not null;"`
	CaseStuSdept  string `json:"casestusdept" gorm:"type:varchar(20) not null;"`
	CaseDiagnosis string `json:"casediagnosis" gorm:"type:varchar(20) not null"`
	CaseBookDate  string `json:"casebookdate" gorm:"type:varchar(100) not null"`
	IsDel         bool   `json:"isdel" form:"isdel" gorm:"type:boolean default false;"`
}

func getJson1(c *gin.Context) (CaseBook, error) {
	json := make(map[string]string, 1024)
	c.ShouldBind(&json)
	fmt.Println(json)

	caseStuID := json["casestuid"]
	caseStuName := json["casestuname"]
	caseStuAge := json["casestuage"]
	caseStuSex := json["casestusex"]
	caseStuSdept := json["casestusdept"]
	caseDiagnosis := json["casediagnosis"]
	caseBookDate := json["casebookdate"]

	age, err := strconv.Atoi(caseStuAge)

	if err != nil {
		return CaseBook{}, err
	}

	casebook := CaseBook{
		CaseStuID:     caseStuID,
		CaseStuName:   caseStuName,
		CaseStuAge:    age,
		CaseStuSex:    caseStuSex,
		CaseStuSdept:  caseStuSdept,
		CaseDiagnosis: caseDiagnosis,
		CaseBookDate:  caseBookDate,
	}

	return casebook, nil
}

func AddCaseBook(c *gin.Context) (CaseBook, int64, error) {
	casebook, err := getJson1(c)

	if err != nil {
		return CaseBook{}, 0, err
	}

	var total int64
	db.Table("case_books").Where("case_stu_id = ?", casebook.CaseStuID).Count(&total)
	if total == 0 {
		err := db.Table("case_books").Create(&casebook).Error
		return casebook, total, err
	}

	return casebook, total, nil
}

func UpdateCaseBook(c *gin.Context) (CaseBook, error) {
	casebook, err := getJson1(c)
	fmt.Println(err)
	if err != nil {
		return CaseBook{}, err
	}
	err = db.Table("case_books").Where("case_stu_id = ?", casebook.CaseStuID).Updates(&casebook).Error
	return casebook, err
}

func GetAllCaseBook(CaseBookParam map[string]interface{}) ([]CaseBook, int64, error) {
	var caseData []CaseBook
	pagenum := CaseBookParam["pagenum"].(int)
	pagesize := CaseBookParam["pagesize"].(int)
	query := CaseBookParam["query"].(string)

	if query == "" {
		var total int64

		err := db.Table("case_books").Where("is_del = false").Count(&total).Error
		if err != nil {
			return caseData, 0, err
		}

		err = db.Table("case_books").Where("is_del = false").Offset((pagenum - 1) * pagesize).Limit(pagesize).Find(&caseData).Error
		return caseData, total, err
	}
	caseID := query

	var casebook CaseBook
	err := db.Table("case_books").Where("case_stu_id = ?", caseID).First(&casebook).Error
	caseData = append(caseData, casebook)
	return caseData, 1, err
}

func DelCaseBook(c *gin.Context) error {
	id := c.Params.ByName("id")
	err := db.Table("case_books").Where("case_stu_id = ?", id).Update("is_del", true).Error
	return err
}
